const app = getApp()
const api = app.globalData.api
Page({
    data: {
        item: [],
        detail: {},
        dateField: '',
        workshopOption: {},
        lineOption: {},
        typeOption: [],
        materialOption: {},
        unitOption: [],
        bomOption: {},
        routingOption: {},
        sourceIdOption: {},
        numOption: {}
    },
    // 选择订单
    getOrder: async function () {
        let res = await api.curlGet('?cla=machiningOrder&fun=getOrder')
        if (res.code === 1) {
            this.setData({
                saleOrderNoOption: res.data
            })
        }
    },
    // 选择物料
    getMaterial: async function (params = {}) {
        let res = await api.curlPost('?cla=machiningOrder&fun=getMaterial', params)
        if (res.code === 1) {
            let materialOption = {}, unitOption = {}, bomOption = {}, numOption = {}, sourceIdOption = {}, routingOption = {}
            const resp = res.data
            for (const key in resp) {
                materialOption[key] = resp[key].material
                unitOption[key] = resp[key].unit
                bomOption[key] = resp[key].bom
                numOption[key] = resp[key].num
                sourceIdOption[key] = resp[key].id
                routingOption[key] = resp[key].routing
            }
            wx.setStorageSync('materialOption', materialOption)
            wx.setStorageSync('unitOption', unitOption)
            wx.setStorageSync('bomOption', bomOption)
            wx.setStorageSync('numOption', numOption)
            wx.setStorageSync('sourceIdOption', sourceIdOption)
            wx.setStorageSync('routingOption', routingOption)
            this.setData({
                materialOption: Object.values(materialOption)
            })
        }
    },
    formSubmit: async function (e) {
        let data = e.detail.value,
            id = data.id ? data.id : api.suiji(),
            machiningOrderDetailData = wx.getStorageSync('machiningOrderDetailData') ? wx.getStorageSync('machiningOrderDetailData') : {}
        if (id) {
            // 初始化部分值
            data['num'] = api.initNaN(data, 'num')
            data['id'] = id
            machiningOrderDetailData[id] = data
            api.showToast('选择成功')
            wx.setStorageSync('machiningOrderDetailData', machiningOrderDetailData)
            wx.navigateBack({
                delta: 1
            })
        }
    },
    showPopup: function (obj) {
        let popup = obj.currentTarget.dataset.show
        if (popup) {
            this.setData({
                [popup]: true
            });
        }
    },
    onDisplay: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        this.setData({
            calendar: true,
            prefix: prefix,
        })
    },
    // 确认选择
    onConfirm: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        let field = (prefix ? prefix + '.' : '') + obj.field
        this.setData({
            [obj.target]: false,
            [field]: this.funRouting(obj.field, e.detail, prefix),
        })
    },
    // 关闭
    onClose: function (e) {
        let obj = e.currentTarget.dataset
        if (obj) {
            this.setData({[obj.target]: false})
        }
    },
    goto(e) {
        return api.goto(e)
    },
    funRouting: function (type, detail) {
        const array = detail.value
        switch (type) {
            // 选项与值相等
            case 'workshop':
                this.setData({
                    lineOption: this.getLine('lineOption', array['text'])
                })
                return array['text']
            // 选项与值为key value
            case 'lineName':
                this.setData({
                    ['item.lineId']: array['value']
                })
                return array['text']
            // 选择订单
            case 'saleOrderNo':
                // 先清空
                this.setData({
                    ['item.orderId']: array['value'],
                    ['item.material']: '',
                    ['item.unit']: '',
                    ['item.num']: 0,
                    ['item.bomVersion']: '',
                    ['item.routingName']: '',
                    ['item.routingId']: '',
                    ['item.sourceId']: '',
                })
                // 再赋值
                this.getMaterial({orderId: array['value']})
                return array['text']
            // 选择物料
            case 'material':
                const id = array['value']
                this.setData({
                    ['item.materialId']: id,
                    unitOption: wx.getStorageSync('unitOption')[id],
                    bomOption: wx.getStorageSync('bomOption')[id],
                    routingOption: wx.getStorageSync('routingOption')[id],
                    ['item.num']: wx.getStorageSync('numOption')[id],
                    ['item.sourceId']: wx.getStorageSync('sourceIdOption')[id],
                })
                return array['text']
            case 'bomVersion':
                this.setData({
                    ['item.bomId']: array['value']
                })
                return array['text']
            case 'routingName':
                this.setData({
                    ['item.routingId']: array['value']
                })
                return array['text']
            // 日期
            case 'startDate':
            case 'endDate':
                return api.formatDate(detail)
            // 单字符串
            default:
                return detail.value
        }
    },
    onLoad: async function (obj) {
        // const option = obj.data ? api.jsonToArray(obj.data) : []
        const item = obj.now ? api.jsonToArray(obj.now) : {}
        this.setData({
            item: item
        })
        await this.getOrder()
        await this.getMaterial()
    }
});